var prjId = null;//项目id
var app = null;//vue实例

mui.ready(function(){

    //获取项目id
    prjId = location.search.substring(1);

    //创建vue实例
    app = new Vue({
        el:'#app',
        data:{
            itemName:'',
            itemCompany:'',
            itemUser1:'',
            itemDepartment:'',
            user1Phone:'',
            itemText:'',

            updatetime:'',
            s1:'',

            itemUser2:'',
            user2Department:'',
            user2Phone:'',
            user2Text:'',

            itemUser3:'',
            user3Department:'',
            user3Phone:'',
            user3Text:'',

            itemPlan:'',
            itemReport:'',

            itemImg:[]
        },
        methods:{
            formatTime:function(strTime){ //格式化时间
                var date = new Date(strTime);
                return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
            },
            handleUpdate:function(){ //系统发文修改
                mui.openWindow({
                    url:'./historyPrj_system_update.html?'+prjId,
                    id:'historyPrj_system_update'
                });
            },
            getDataByAjax:function(){
                //获取vue实例
                var self = this;
                //获取数据
                jQuery.ajax({
                    url:OPENAPI.selectByItemId,
                    data:{
                        itemID:prjId
                    },
                    type:'post',
                    dataType:"json",
                    timeout:5000,
                    success:function(data){
                        var item = data.item;
                    
                        self.itemName = item.itemName;
                        self.itemCompany = item.itemCompany;
                        self.itemUser1 = item.itemUser1;
                        self.itemDepartment = item.itemDepartment;
                        self.user1Phone = item.user1Phone;
                        self.itemText = item.itemText;

                        self.updatetime = self.formatTime( item.updatetime );
                        self.s1 = item.s1;

                        self.itemUser2 = item.itemUser2 || '暂无';
                        self.user2Department = item.user2Department || '暂无';
                        self.user2Phone = item.user2Phone || '暂无';
                        self.user2Text = item.user2Text || '暂无';

                        self.itemUser3 = item.itemUser3 || '暂无';
                        self.user3Department = item.user3Department || '暂无';
                        self.user3Phone = item.user3Phone || '暂无';
                        self.user3Text = item.user3Text || '暂无';

                        self.itemPlan = item.itemPlan || '暂无';
                        self.itemReport = item.itemReport || '暂无';

                        if( item.itemImg != '' && item.itemImg != null && item.itemImg != undefined ){
                            self.itemImg = ( item.itemImg ).split('★'); 
                        }
                    },
                    error:function(error){
                        mui.toast('网络连接异常');
                    }
                });
            },
            storageChange:function(key,fn,value){
                //key为要存储的名字，fn为触发storage后要执行的方法，value可以自己设置存的值，可以利用这个值跨页面传参
                var val = value ? JSON.stringify(value) : new Date().getTime()//为value设置默认值
                localStorage.setItem(key,val)
                window.addEventListener('storage',function(e){
                    if(e.key == key){//判断是否是目标值发生改变
                        fn(e.newValue,e.oldValue)//执行fn，返回新值和旧值
                    }
                })
            }
        },
        created:function(){
            this.getDataByAjax();
        },
        mounted:function(){
            setTimeout(function(){
                mui.previewImage();
            }, 3000);
        }
    });

    //监听页面修改后是否刷新
    app.storageChange('sysupdate',function(){
        //重新渲染页面数据
        app.getDataByAjax();
    });

    mui('.mui-scroll-wrapper').scroll({
        indicators: true //是否显示滚动条
    });


});